﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetDxComponents.ExtenderProviders.Options;

namespace MiniKsiegowosc.Ustawienia
{
    public partial class FormOpcje : ProgNetAppBlocks.Base.ProgNetForm
    {
        public FormOpcje()
        {
            InitializeComponent();
        }
        protected List<string> Errors = new List<string>();

        private void modeProvider1_CurrentModeChanged(object sender, EventArgs e)
        {

        }

        protected virtual void ToggleName()
        {
            if (changeDetercorProvider1.HasChanges)
            {
                if (!this.Text.EndsWith(" *"))
                {
                    this.Text += " *";
                }
            }
            else
            {
                if (this.Text.EndsWith(" *"))
                {
                    this.Text = this.Text.XReplace(" \\*$", "");
                }
            }
        }
        
        private void changeDetercorProvider1_HasChangesChanged(object sender, EventArgs e)
        {
            if (this.IsAppRunning())
            {
                modeProvider1.RefreshObjects(bbiZapisz);
                ToggleName();
            }
        }

        private void bbiOdswiez_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshEntry();
        }

        private void bbiEdytuj_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ToggleEdit();
        }

        private void bbiZapisz_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DoSave();
        }
        #region protected methods
        protected void AddError(string error)
        {
            if (!Errors.Contains(error))
                Errors.Add(error);
        }

        protected bool DoValidate()
        {
            ClearValidation();
            ValidateAll();
            return Errors.Count == 0;
        }
        #endregion
        protected virtual void ValidateAll()
        {

        }

        protected virtual void ClearValidation()
        {
            dxErrorProvider1.ClearErrors();
            Errors.Clear();
        }
        protected virtual bool ToggleEdit()
        {
            bool result = true;

            if (modeProvider1.CurrentMode == ProgNetDxComponents.ExtenderProviders.FormMode.Read)
            {
                modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Edit;
            }
            else
            {
                if (!changeDetercorProvider1.HasChanges)
                    FinishEdit();
                if (changeDetercorProvider1.HasChanges)
                {
                    DialogResult dlg = this.ShowDxYNCQuestionDialog("Zapisać zmiany?");
                    if (dlg == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (DoSave())
                            modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
                        result = false;
                    }
                    else if (dlg == System.Windows.Forms.DialogResult.No)
                    {
                        OpenEntry();
                        result = true;
                        modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
                    }
                    else
                        result = false;
                }
                else
                    modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
            }
            return result;
        }
        protected virtual bool RefreshEntry()
        {
            bool result = true;
            FinishEdit();
            if (changeDetercorProvider1.HasChanges)
            {
                DialogResult dlg = this.ShowDxYNCQuestionDialog("Zapisać zmiany?");
                if (dlg == System.Windows.Forms.DialogResult.Yes)
                {
                    result = DoSave();
                }
                else if (dlg == System.Windows.Forms.DialogResult.No)
                {
                    modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
                    result = true;
                }
                else
                    result = false;
            }
            if (result)
                DoOpenEntry();
            return result;
        }
        protected virtual void FinishEdit()
        {
            this.FinishEditBindings();
        }
        protected virtual bool DoSave()
        {
            bool result = true;
            FinishEdit();
            if (DoValidate())
            {
                if (Save())
                {
                    modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
                    changeDetercorProvider1.ResetAll();
                        DoOpenEntry();
                }
                else
                {
                    this.ShowDxErrorDialog("Błąd zapisu!");
                    result = false;
                }
            }
            else
            {
                this.ShowDxErrorDialog(Errors.AsSingleString());
                result = false;
            }
            return result;
        }
        protected virtual bool OpenEntry()
        {
            changeDetercorProvider1.LockContext();
            optionProvider1.RefreshAllObjects();
            changeDetercorProvider1.UnLockContext();
            changeDetercorProvider1.ResetAll();
            return true;
        }
        protected virtual void DoOpenEntry()
        {
            if (OpenEntry())
            {
                changeDetercorProvider1.ResetAll();
                this.Text = "Opcje";
            }
            else
            {
                this.ShowDxErrorDialog("Błąd otwarcia rekordu.");
                modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.View;
            }
        }
        protected virtual bool Save()
        {
            bool result = OptionCache.SaveAllOptions();
            return result;
        }

        protected virtual void ModeSettings_CustomGetControlModes(object sender, ProgNetDxComponents.ExtenderProviders.Settings.ModeSettingsEventArgs e)
        {
            if (sender == bbiZapisz)
                e.IsReadOnly = !changeDetercorProvider1.HasChanges;
        }
    }
}
